package com.irun.sm.ui.demo.ui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.irun.sm.ui.demo.utils.ActivityUtils;
import com.irun.sm.ui.demo.utils.BitmapCache;

/***
 * @author huangsm
 * @date 2013-1-28
 * @email huangsanm@gmail.com
 * @desc 瀑布流
 * linearlayout作为容器填充元素，然后再把linearlayout放置scrollview里面
 * http://www.eoeandroid.com/thread-252177-1-1.html
 */
public class WaterFallActivity extends Activity {
	
	private final static String FILENAME = "images";
	//页大小
	private final static int PAGE_SIZE = 16;

	//滚动
	private ScrollView mScrollView;
	//容器
	private LinearLayout mContainerLinearLayout;
	//第一页
	private int mCurrentPage = 0;
	//列
	private int mColumn = 4;
	private String[] mArrays;
	private AssetManager mAssetManager;
	private int mColumnWidth;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//去除标题栏
		ActivityUtils.requestNotTitleBar(this);
		
		setContentView(R.layout.waterfall_layout);
		
		mColumnWidth = getWindowManager().getDefaultDisplay().getWidth() / mColumn;
		
		try {
			mAssetManager = getAssets();
			mArrays = mAssetManager.list(FILENAME);
		} catch (IOException e) {
			e.printStackTrace();
		}
		setupView();
	}
	
	
	private void setupView(){
		mScrollView = (ScrollView) findViewById(R.id.scrollview);
		mScrollView.setOnTouchListener(mOnTouchListener);
		mContainerLinearLayout = (LinearLayout) findViewById(R.id.content);
		
		//计算每个linearlayout的宽度
		for (int i = 0; i < mColumn; i++) {
			LinearLayout layout = new LinearLayout(this);
			layout.setLayoutParams(new LinearLayout.LayoutParams(mColumnWidth, LinearLayout.LayoutParams.WRAP_CONTENT));
			layout.setOrientation(LinearLayout.VERTICAL);
			layout.setPadding(2, 2, 2, 2);
			//加入容器
			mContainerLinearLayout.addView(layout);
		}
		
		addItems(mCurrentPage);
	}
	
	//添加图片
	private void addItems(int currentPage){
		final int length = mArrays.length;
		int j = 0;
		for (int i = currentPage * PAGE_SIZE; i < PAGE_SIZE * (currentPage + 1) && i < length; i++) {
			j = j >= mColumn ? 0 : j;
			addView(mArrays[i], j ++);
		}
	}
	
	private void addView(String fname, int index){
		System.out.println("index:" + index);
		final ImageView image = (ImageView) getLayoutInflater().inflate(R.layout.image, null);
		image.setImageResource(R.drawable.ic_launcher);
		LinearLayout ll = (LinearLayout) mContainerLinearLayout.getChildAt(index);
		ll.addView(image);
		
		//加载图片
		new ImageLoader(mAssetManager, image).execute(FILENAME + "/" + fname);
	}
	
	private OnTouchListener mOnTouchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if(mContainerLinearLayout.getMeasuredHeight() <= mScrollView.getScrollY() + mScrollView.getHeight()){
				addItems(++mCurrentPage);
			}
			return false;
		}
	};
	
	class ImageLoader extends AsyncTask<String, Void, Bitmap> {

		private AssetManager mAssetManager;
		private ImageView mView;
		public ImageLoader (AssetManager assetManager, ImageView image){
			mAssetManager = assetManager;
			mView = image;
		}
		
		@Override
		protected Bitmap doInBackground(String... params) {
			String fileName = params[0];
			Bitmap bitmap = null;
			try {
				BitmapFactory.Options opts = new BitmapFactory.Options();
				opts.inTempStorage = new byte[16 * 1024];
				bitmap = BitmapCache.getInstance().get(fileName, 
								BitmapFactory.decodeStream(mAssetManager.open(fileName)));
			} catch (IOException e) {
				e.printStackTrace();
			}
			return bitmap;
		}
		
		@Override
		protected void onPostExecute(Bitmap result) {
			if(result != null){
				int width = result.getWidth();
				int height = result.getHeight();
				LayoutParams lp = mView.getLayoutParams();
				//计算mColumnWidth比例下显示的height
				lp.height = (height * mColumnWidth) / width;
				mView.setLayoutParams(lp);
				mView.setImageBitmap(result);
			}
		}
	}
}
